package com.liaoban.app.ui.mine;

import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dalong.recordlib.RecordVideoUtils;
import com.liaoban.app.R;
import com.liaoban.app.adapter.AlbumAdapter;
import com.liaoban.app.adapter.MyVideoAdapter;
import com.liaoban.app.api.type.Img;
import com.liaoban.app.api.type.MyVideo;
import com.liaoban.app.api.type.request.ImgDelRequest;
import com.liaoban.app.api.type.request.ImgListRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseListActivity;
import com.liaoban.app.util.CommonUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ybc on 2018/7/13 0013.
 * 我的视频
 */
public class MyVideoActivity extends BaseListActivity<MyVideo> {
    private List<MyVideo> mVideos = new ArrayList<>(); //保存所有视频

    @Override
    protected void setRecyclerView(RecyclerView recyclerView) {
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    public String getNewTitle() {
        return getString(R.string.my_video);
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return new MyVideoAdapter(null);
    }

    @Override
    public void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        switch (view.getId()) {
            case R.id.ic_del:
                MyVideo myVideo = (MyVideo) baseQuickAdapter.getData().get(position);
                showRunningDialog(R.string.committing);
                ImgDelRequest imgDelRequest = new ImgDelRequest();
                imgDelRequest.setImgId(myVideo.getImgId() + "");
                startTask(CommonBiz.getInstance().videoDelete(imgDelRequest), baseResponse -> {
                    ToastHelper.show(this, baseResponse.msg);
                    if (1 == baseResponse.code) {
                        baseQuickAdapter.remove(position);
                        mVideos.remove(position);
                    }
                });
                break;
            case R.id.layout_add:
                showSheetVideoDialog();
                break;
        }
    }

    @Override
    public void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        MyVideo myVideo = (MyVideo) baseQuickAdapter.getData().get(position);
        CommonUtils.startNeVideo(this, myVideo.getVideoSrc());
    }

    @Override
    public void getData() {
        ImgListRequest imgListRequest = new ImgListRequest();
        imgListRequest.setWuserId(ConfigHelper.getString(this, ConfigKey.USER_ID));
        startTask(CommonBiz.getInstance().videoList(imgListRequest), listBaseResponse -> {
            doSuc(listBaseResponse.data, listBaseResponse.totalPageCount);
        });
    }

    @Override
    //数据请求成功后的结果处理
    public void doSuc(List<MyVideo> rows, int totalPageCount) {
        swipeLayout.setRefreshing(false);
        swipeLayout.setEnabled(true);
        mAdapter.loadMoreComplete();
        if (mPageIndex == 1) {
            if (mVideos.size() > 0) {
                mVideos.clear();
            }
        }
        mVideos.addAll(rows);
        List<MyVideo> tempVideos = new ArrayList<>();
        tempVideos.addAll(mVideos);
        tempVideos.add(new MyVideo()); //最后面添加一个添加图片的图标
        mAdapter.setNewData(tempVideos);
        if (mPageIndex++ >= totalPageCount) {
            mAdapter.loadMoreEnd(false);
        }
    }

    @Override
    protected void uploadVideoSuccessful(String videoPath) {
        super.uploadVideoSuccessful(videoPath);
        if (isVideoLarge(videoPath)) {
            ToastHelper.show(this, "视频太大");
            return;
        }
        showRunningDialog(R.string.committing);
        File videoFile = new File(videoPath);
        Bitmap firstBitmap = RecordVideoUtils.getVideoFristImage(videoPath);
        String firstPicPath = CommonUtils.saveBitmap(this, firstBitmap);
        File firstImg = new File(firstPicPath);
        startTask(CommonBiz.getInstance().videoUpload(firstImg, videoFile), baseResponse -> {
            ToastHelper.show(this, baseResponse.msg);
            mVideos.add(0, baseResponse.data);
            List<MyVideo> tempVideos = new ArrayList<>();
            tempVideos.addAll(mVideos);
            tempVideos.add(new MyVideo()); //最后面添加一个添加图片的图标
            mAdapter.setNewData(tempVideos);
        });
    }

    @Override
    protected boolean isPage() {
        return false;
    }
}
